package it.xiuxian.homepage.fragment

import android.annotation.SuppressLint
import android.util.Log
import android.view.View.OnClickListener
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bytedance.sdk.openadsdk.*
import com.bytedance.sdk.openadsdk.TTAdNative.RewardVideoAdListener
import com.bytedance.sdk.openadsdk.TTRewardVideoAd.RewardAdInteractionListener
import it.xiuxian.homepage.R
import it.xiuxian.homepage.bean.*
import it.xiuxian.homepage.databinding.HomepageNewBinding
import it.xiuxian.homepage.presenter.HomePageFragmentPresenter
import it.xiuxian.lib.base.ArouterAddress
import it.xiuxian.lib.base.BaseFragment
import it.xiuxian.lib.bean.NewPersonBean
import it.xiuxian.lib.utils.*
import it.xiuxian.lib.utils.adapter.SlimAdapter
import it.xiuxian.lib.utils.adapter.SlimInjector
import it.xiuxian.lib.utils.adapter.injector.IViewInjector
import it.xiuxian.xingqiu.bean.SuanLiBean
import kotlinx.android.synthetic.main.homepage_new.*
import kotlinx.android.synthetic.main.homepage_new.view.*
import java.util.*


@Route(path = ArouterAddress.HOMEPAGEFRAGMENT)
class HomePageFragment :BaseFragment<HomePageFragmentPresenter>(){
    var adapter1: SlimAdapter?=null
   lateinit var binding:HomepageNewBinding
    override val layoutId: Int
        get() = R.layout.homepage_new
    override val presenter: HomePageFragmentPresenter
        get() = HomePageFragmentPresenter()

    override fun isEmpty(textView: TextView?): Boolean {
        return false
    }

        var i =1
    var j=1
    override fun initView(){
       // LogUtils.e("homepage加载了", Throwable())
       // ToastUtil.showShort("homepage加载了")
        ARouter.getInstance().inject(this)
        binding =  getViewDataBinding<HomepageNewBinding>()
        binding.banner.setImageLoader(GlideRoundImageLoader())
        binding.banner.setDelayTime(2000)
        LiveDataBus.get().with(RxBusCode.FACE).observe(this, androidx.lifecycle.Observer {
            mPresenter!!.getHomePage()
        })
        binding.banner.setOnBannerClickListener {
            if(content[it - 1].keys.contains(1)) {
                ARouter.getInstance().build(ArouterAddress.BANNERDETAILACTIVITY).withString(
                    "content",
                    content[it - 1][1]
                ).withString("flag", "1").navigation()
            }else if(content[it - 1].keys.contains(2)){
                ARouter.getInstance().build(ArouterAddress.BANNERDETAILACTIVITY).withString(
                    "content",
                    content[it - 1][2]
                ).withString("flag", "2").navigation()
            }else if(content[it - 1].keys.contains(3)){
                if(status==3){
                    ToastUtil.showShort("请先领取工作证")
                }else{
                    ARouter.getInstance().build(ArouterAddress.WORKACTIVITY).navigation()
                }
            }else if(content[it - 1].keys.contains(4)){
                ARouter.getInstance().build(ArouterAddress.GONGXIANACTIVITY).navigation()
            }
        }
        binding.llZixun.setOnClickListener { ARouter.getInstance().build(ArouterAddress.ZIXUNACTIVITY).withString(
            "erweima",
            erweima
        ).navigation() }
        binding.rvZixun.layoutManager = LinearLayoutManager(requireActivity())
        LiveDataBus.get().with(RxBusCode.QIANDAO).observe(this, androidx.lifecycle.Observer {
            mPresenter!!.getHomePage()
        })
        adapter1 = SlimAdapter.create().register<Detail>(R.layout.homepage_zixun_second_item, object : SlimInjector<Detail> {
            override fun onInject(
                data: Detail?,
                injector: IViewInjector<out IViewInjector<*>>?
            ) {

                injector!!.text(R.id.tv_newcontent, data!!.liulannum + "人次阅读")
                    .text(R.id.tv_name, data.biaoti)
                    .text(R.id.tv_time,data.createdate)
                    .clicked(R.id.ll_zong, OnClickListener {
                        var i = data.id
                        ARouter.getInstance().build(ArouterAddress.ZIXUNDETAILACTIVITY)
                            .withString(
                                "erweima",
                                erweima
                            ).withString("imgurl",data.imgurl).withString("biaoti",data.biaoti).withString("id", i).navigation()
                    })


                ImageUtil.setImage(data.imgurl, injector.findViewById<ImageView>(R.id.image))


            }
        }).attachTo(binding.rvZixun)

    }

    override fun loadData(){
        mPresenter!!.getBanner()
        mPresenter!!.getHomePage()
        mPresenter!!.getMonGongxian()
        mPresenter!!.getZongGongxian()
        mPresenter!!.getZiXun()
        mPresenter!!.getList("1","1")
       // super.loadData()
    }
    /*override fun initData() {
        //super.initData()

    }*/
    private var status = 2
    private var get= false
    var adapter3: SlimAdapter?=null
    var adapter2: SlimAdapter?=null
    override fun initEvent(){
            binding.imagezhao1.setOnClickListener {
                ARouter.getInstance().build(ArouterAddress.BANNERACTIVITY).withSerializable("newlist",newList1).navigation()
            }
        binding.imagezhao2.setOnClickListener {
            ARouter.getInstance().build(ArouterAddress.BANNERACTIVITY).withSerializable("newlist",newList2).navigation()
        }
        binding.imagezhao3.setOnClickListener {
            ARouter.getInstance().build(ArouterAddress.BANNERACTIVITY).withSerializable("newlist",newList3).navigation()
        }
            binding.llGongzuo.setOnClickListener{
                if(status==3){
                    ToastUtil.showShort("请先领取工作证")
                }else{
                    ARouter.getInstance().build(ArouterAddress.WORKACTIVITY).navigation()
                }

            }
            binding.llDaka.setOnClickListener{
                if(Utils.isFastClick()){
                        if(status==2||status==1){
                            ARouter.getInstance().build(ArouterAddress.QIANDAOACTIVITY).navigation()
                        }else{
                            ToastUtil.showShort("请先领取工作证")
                        }

                }

               // ARouter.getInstance().build(ArouterAddress.QIANDAOACTIVITY).navigation()
            }
        binding.llGaoji.setOnClickListener{
            ToastUtil.showShort("暂未开放")
        }
        binding.llQuanbu.setOnClickListener { ToastUtil.showShort("暂未开放") }
        binding.llShipin.setOnClickListener { ToastUtil.showShort("暂未开放") }
        //binding.llShejiao.setOnClickListener { ToastUtil.showShort("暂未开放")}
        binding.llLiaotian.setOnClickListener { ToastUtil.showShort("暂未开放") }
        binding.ivFenxiang.setOnClickListener {
            ARouter.getInstance().build(ArouterAddress.GONGGAOACTIVITY).navigation()
        }
      /*  binding.llYuepai.setOnClickListener { ARouter.getInstance().build(ArouterAddress.PAIHANGBANGACTIVITY).withString(
            "flag",
            "1"
        ).navigation() }
        binding.llZongpai.setOnClickListener { ARouter.getInstance().build(ArouterAddress.PAIHANGBANGACTIVITY).withString(
            "flag",
            "2"
        ).navigation() }*/
        }
    private var list = ArrayList<String>()
    private var content = ArrayList<HashMap<Int, String>>()
    fun setUI(data: BannerBean){
        list.clear()
        content.clear()
        for(i in data.list){
            list.add(i.imgpath)

            if(i.leixing==1){
                var map = HashMap<Int, String>()
                map[1] = i.url
                content.add(map)
            }else if(i.leixing==2){
                var map = HashMap<Int, String>()
                map[2] = i.content
                content.add(map)
            }else if(i.leixing==3){
                var map = HashMap<Int, String>()
                map[3] = i.content
                content.add(map)
            }else if(i.leixing==4){
                var map = HashMap<Int, String>()
                map[4] = i.content
                content.add(map)
            }
        }
        binding.banner.setImages(list)
        binding.banner.start()
    }
    var erweima:String?=null

    fun getXinXI(data: NewPersonBean){
        if (data.shimingstate==1){
            binding.llGongzuo.isEnabled = true
            binding.llGongzuo.isClickable = true
            if(data.qiandaostate=="1"){
               /* binding.llDaka.isEnabled = false
                binding.llDaka.isClickable = false*/
                status = 1
            }else{
                binding.llDaka.isEnabled = true
                binding.llDaka.isClickable = true
                status = 2
            }
        }else{
           /* binding.llDaka.isEnabled = false
            binding.llDaka.isClickable = false
            binding.llGongzuo.isEnabled = false
            binding.llGongzuo.isClickable = false*/
            status = 3
        }
        SPUtil.put("erweima",data.yaoqingerweima)
        // erweima = data.yaoqingerweima
    }
//var alist = ArrayList<Detail>()
    @SuppressLint("SetTextI18n")
    fun setZiXun(data: ZiXunBean){
      //   alist.clear()
        if(data.list.isNotEmpty()){

            adapter1!!.updateData(data.list)
        }

    }

    fun updateUI(msg: String){
        ToastUtil.showShort(msg)
        mPresenter!!.getHomePage()
    }

    override fun onDestroyView(){
        super.onDestroyView()
       // LogUtils.e("homepage is onDestroyView", Throwable())
        //ToastUtil.showShort("homepage is destroyview")
    }
    var newList1 = ArrayList<String>()
    var newList2 = ArrayList<String>()
    var newList3 = ArrayList<String>()
    fun geData(data: SuanLiBean){
            if(data.list.size>=3){
                ImageUtil.setRoundImage(data.list[0].imgurl,binding.imagezhao1)
                binding.tvTitle1.text = data.list[0].message
                newList1.addAll(data.list[0].imageList)
                ImageUtil.setRoundImage(data.list[1].imgurl,binding.imagezhao2)
                binding.tvTitle2.text = data.list[1].message
                newList2.addAll(data.list[1].imageList)
                ImageUtil.setRoundImage(data.list[2].imgurl,binding.imagezhao3)
                binding.tvTitle3.text = data.list[2].message
                newList3.addAll(data.list[2].imageList)

            }else{
                ToastUtil.showShort("数据不足,请添加照片墙")
            }
    }

    /*  fun setGongXian(data: MonGongXianBean) {
          ImageUtil.setImage(data.list[0].faceurl, binding.image1)
          adapter3!!.updateData(if (data.list.size >= 3) data.list.subList(0, 3) else data.list)
      }

      fun setZong(data: MonGongXianBean){
          ImageUtil.setImage(data.list[0].faceurl, binding.image2)
          adapter2!!.updateData(if (data.list.size >= 3) data.list.subList(0, 3) else data.list)
      }*/


}




